<cfset BASE = CreateObject("component", "#session.rootDir#.cfc.base") />

<cfset VARIABLES.AddedUsers = ArrayNew(1) />
<cfset VARIABLES.DisabledUsers = ArrayNew(1) />

 
<cfobject type="dotnet" name="objQuery" class="ADTools.Query" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">  
 
 
<cfmail to="bfrick@scf.cc" from="ANMCSync@scf.cc" subject="ANMC Sync Report" type="html">
	
	<!--- #1 --->
	<!--- Search all Active Rosetta ANMC users and check to see if they are still active using GUID --->
	<cfquery name="qryGetANMCAccounts" datasource="#Session.DataSourceRosetta#">
		scf_GetActiveAccounts 0
	</cfquery>
	
	<cfloop query="qryGetANMCAccounts">
		<cfif qryGetANMCAccounts.AD_GUID NEQ "" and qryGetANMCAccounts.UserAccount NEQ "">
			<cfset strGUID = qryGetANMCAccounts.AD_GUID>
			<cfset intRosettaID = qryGetANMCAccounts.RosettaID>
			<cfset strUserID = objQuery.GetProperty("distinguishedName", "10.100.1.101", "", "#strGUID#", "")>
			
			<cfif isdefined("strUserID") and isnumeric(strUserID) NEQ 1>
				<!--- THESE ARE FINE.  NO ACTION NEEDED --->
			<cfelse>
				#intRosettaID# #strGUID# ------ ANMC DISABLE ------<BR />
				<!--- DISABLE --->
				<cfquery name="qryDisableGUID" datasource="#Session.DataSourceRosettaStone#">
					scf_DisableRosettaUser #intRosettaID#
				</cfquery>
			</cfif>
		</cfif>
	</cfloop>
	
	
	
	
	<!--- #2 --->
	<!--- Get the List of Employees from LDAP (for this purpose, considered to be the most current) --->
	<cfldap action="QUERY"
		name="qryANMCEmployeesFromLDAP"
		SCOPE="subtree"
		start="dc=SouthcentralFoundation,dc=com"
		filter="(&(objectclass=contact)(company=ANMC))"
		server="10.100.1.9"
		attributes="company,dn,givenname,sn,displayName,mail,telephonenumber,sAMAccountName,physicalDeliveryOfficeName"
		sort="displayName"
		SORTCONTROL="nocase"
		username="scf\Userlist"
		password="DefinedMan" />
		
	<!---cfdump var="#qryANMCEmployeesFromLDAP#"--->
	
	
	<!--- #3 --->
	<!--- Iterate the list of ANMC employees and Update or Add as appropriate --->
	<cfloop query="qryANMCEmployeesFromLDAP">
		<cfset intNonSCFMasterID = 0>
		<cfset strMail = qryANMCEmployeesFromLDAP.Mail>
		<cfset strUserID = ListGetAt(strMail, 1, "@")>
		
		<cfset strGUID = objQuery.GetGUID("", "#strMail#", "10.100.1.101")/> <!--- All current ANMC contacts should have a GUID --->
		
		<cfquery name="qryGetRosettaInfo" datasource="#Session.DataSourceRosetta#">
			scf_GetRosettaInfo '', 0, '#strGUID#'
		</cfquery>
		
		<cfif qryGetRosettaInfo.recordcount GT 0>
			<cfif #strUserID# NEQ qryGetRosettaInfo.UserAccount and left(qryGetRosettaInfo.UserAccount, 4) NEQ 'ANMC'>
				#strGUID# OLD: #qryGetRosettaInfo.UserAccount# NEW: #strUserID#  -------- EXISTING --------- UPDATE<br /> 
				<cfquery name="qryUpdateAccount" datasource="#Session.DataSourceRosetta#">
					scf_UpdateUserAccount '#strUserID#', #qryGetRosettaInfo.RosettaID#
				</cfquery>
			</cfif>
		<cfelse>
			No record --- ADD --- #givenname#, #sn# ---- #mail#
			<BR />
			<cfset strPhone = BASE.CleanPhone(qryANMCEmployeesFromLDAP.telephonenumber)>

			<cfquery name="qryCreateNonSCFMasterRecord" datasource="#Session.DataSourceRosetta#">
				scf_CreateNonSCFMasterRecord '#replace(givenname," ","")#', '#replace(sn," ","")#', NULL, NULL, NULL, 
					'#mail#', <cfif telephonenumber EQ ''>NULL<cfelse>'#strPhone#'</cfif>, 
					'#company#', '#physicalDeliveryOfficeName#', NULL, 1
			</cfquery>
			<cfset intNonSCFMasterID = qryCreateNonSCFMasterRecord.NewID>
			<cfquery name="qryCreateRosettaRecord" datasource="#Session.DataSourceRosetta#">
				scf_CreateRosettaRecord '#strUserID#', '#strGUID#', NULL, NULL, #intNonSCFMasterID#                                
			</cfquery>     
		</cfif>
		
			
	</cfloop>


</cfmail>